home *** CD-ROM | disk | FTP | other *** search
/ Inside Mac Games Volume 3 #12 / IMG 33 Dec 1995.iso / Important Files / MGW1Codeƒ / MGWUtilities1.c < prev   
Text File  |  1995-06-29  |  15KB  |  352 lines

  1. //==============================================================================================\\
  2. //        -----------------------------------------------------------------------------------        \\
  3. //        MGWUtilities1.c version 1.0.0    copyright © 1993…1995 Jamie McCornack, john calhoun        \\
  4. //        -----------------------------------------------------------------------------------        \\
  5. //         Alerts and other utilities for Macintosh GameWriter 1.0.0, a training program…            \\
  6. //        …for beginning Mac game programmers. MGW1 includes MGWExterns1.h, MGWUtilities1.c,…        \\
  7. //        …MGWSound1.c, MGWGraphics1.c, MGWGraphicsBWLite1.c, HelloWorld.rsrc and an assortment…    \\
  8. //        …of demo programs; projects HelloWorld1.π etc. and source code files HelloWorld1.c etc.    \\
  9. //         A tutorial is available in Tricks of the Mac Game Programming Gurus, published…        \\
  10. //        …by Hayden Books, August 1995.                                                            \\
  11. //                                                                                                \\
  12. //        This code is offered by the copyright holders for no fee and for whatever use…            \\
  13. //        …you care to make of it, but we do hope you remember where it came from.                \\
  14. //                                                                                                \\
  15. //        Please send bug reports to MacGameDev at America OnLine.    macgamedev@aol.com            \\
  16. //        Suggestions and observations are also appreciated.                                        \\
  17. //        Updates and upgrades will be available now and then from the above e-mail address.        \\
  18. //==============================================================================================\\
  19.  
  20. // This unit checks and verifies the environment (which model Mac running which version System)…
  21. // …and handles Alerts. It uses 'STR#' resources for alert messages, or your own strings.
  22.  
  23. // IMPORTANT NOTE: Thanks to a bit of late-breaking knowledge, this code departs slightly from…
  24. // …the text of Chapter 0 of Tricks of the Mac Game Programming Gurus. It does not use the variable…
  25. // …thisMac of variable type tMacEnvironment to store start-up information about a paticular Mac's…
  26. // …hardware and system. Since generally, your program will only look at this data once, it's not…
  27. // …worth hanging on to for the whole game.
  28.  
  29. #include "MGWExterns1.h"
  30. #include <palettes.h>
  31.  
  32. GDHandle    gThisGDevice;        // Just this one variable.
  33.  
  34.  
  35. //------------------------------------------------------------ Prototypes
  36.  
  37. Boolean    MacHasColorQD(void);
  38. Boolean    MacIsAtLeastAMacII(void);
  39. Boolean    MacHasSystem7(void);
  40. Boolean    MacHasSystem605(void);
  41. Boolean    MacHasSystem602(void);
  42. Boolean MacIsAtLeastMacII (void);
  43. Boolean CanWeDisplay8Bit (GDHandle theDevice);
  44. void     SwitchToDepth (short newDepth, Boolean doColor);
  45. void     CheckMacEnviro (void);
  46. void    InitToolbox(void);
  47. void    FindOurDevice (void);
  48.  
  49.  
  50. //==============================================================  Functions
  51.  
  52.  
  53. //--------------------------------------------------------------  CenterAlert
  54.  
  55. // Handy function to center any alert within the main monitor. Not dead center, the upper…
  56. // …center, where Mac users (and Apple human interface guidelines) expect them.
  57. // For System 6.x use, since System 7 can center alerts automatically (via ResEdit).
  58.  
  59. void CenterAlert (short theAlertID)
  60. {
  61.     AlertTHndl    alertHandle;
  62.     Rect        theScreen, alertRect;
  63.     short        horiOff, vertOff;            // Distance in pixels to offset the alert.
  64.     Byte        wasState;
  65.     
  66.     theScreen = qd.screenBits.bounds;        // Get main monitor's bounds.
  67.     theScreen.top += LMGetMBarHeight();        // Account for menubar height.
  68.                                             // Get handle to alert resource.
  69.     alertHandle = (AlertTHndl)GetResource('ALRT', theAlertID);
  70.     if (alertHandle != 0L)                    // Make sure we got it!
  71.     {                                        // Remember its "state" (locked, etc.)
  72.         wasState = HGetState((Handle)alertHandle);
  73.         HLock((Handle)alertHandle);            // Lock it so Memory Mnager can't move it.
  74.                                             // Get a copy of its bounds and zero.
  75.         alertRect = (**alertHandle).boundsRect;
  76.         OffsetRect(&alertRect, -alertRect.left, -alertRect.top);
  77.                                             // Calculate offsets for centering bounds.
  78.         horiOff = ((theScreen.right - theScreen.left) - alertRect.right) / 2;    
  79.         vertOff = ((theScreen.bottom - theScreen.top) - alertRect.bottom) / 3;
  80.                                             // And offset the bounds copy.
  81.         OffsetRect(&alertRect, horiOff, vertOff + LMGetMBarHeight());
  82.                                             // Set alerts bounds to our centered rect.
  83.         (**alertHandle).boundsRect = alertRect;
  84.         HSetState((Handle)alertHandle, wasState);    // Set it to its original state.
  85.     }
  86. }
  87.  
  88. //--------------------------------------------------------------  RedAlert
  89.  
  90. // Generic error function.  This is called when there is no hope of recovering from the error.
  91. // A simple alert is brought up and the text passed in the whatGives field of the 'STR#'…
  92. // rRedAlertStringIDs is displayed.  When the user clicks the Okay button, we quit to the Finder.
  93.  
  94. void RedAlert (short    whatGives)
  95. {
  96.     short        doWhat;
  97.     Str255        errorString, errorNumberString;
  98.     
  99.     GetIndString(errorString, rRedAlertStringIDs, whatGives);
  100.         // Set errorString to string number whatGives in 'STR#' resource rRedAlertStringIDs.
  101.     NumToString(whatGives, errorNumberString);
  102.         // Set errorNumberString to decimal digits representing whatGives. Useful if you (the…
  103.         // …programmer) get an unexpected alert message. Check it against the constants and 'STR#'.
  104.     ParamText(errorString, "\p", "\p", "\p");    // Replace ^0 in alert with error mssg.
  105. //    CenterAlert(rRedAlertID);                // Not needed with System 7.x. Use for System 6.x.
  106.     InitCursor();                            // Show arrow cursor.
  107.     doWhat = Alert(rRedAlertID, 0L);        // Bring up alert.
  108.     ExitToShell();                            // Quit to Finder.
  109. }
  110.  
  111. //--------------------------------------------------------------  YellowAlert
  112.  
  113. // This error function is called when the player has an option between quitting or continuing.
  114. // A simple alert is brought up and the text passed in the whatGives field of the 'STR#'…
  115. // rYellowAlertStringIDs is displayed.  When the user clicks the Finder button, we quit…
  116. // …to the Finder, when the user clicks the Play button, we continue to play.
  117.  
  118. void YellowAlert (short    whatGives)
  119. {
  120.     short        doWhat;
  121.     Str255        errorString, errorNumberString;
  122.         
  123.     GetIndString(errorString, rYellowAlertStringIDs, whatGives);
  124.         // Set errorString to string number whatGives in 'STR#' resource rRedAlertStringIDs.
  125.     NumToString(whatGives, errorNumberString);
  126.         // Set errorNumberString to decimal digits representing whatGives. Useful if you (the…
  127.         // …programmer) get an unexpected alert message. Check it against the constants and 'STR#'.
  128.     ParamText(errorString, errorNumberString, "\p", "\p");    // Replace ^0 in alert with error mssg.
  129. //    CenterAlert(rRedAlertID);                // Not needed with System 7.x.
  130.     ShowCursor();                            // Make sure the cursor is showing.
  131.     doWhat = Alert(rYellowAlertID, 0L);        // Bring up alert, find out which item was chosen.
  132.     if (doWhat == kYellowExitItem)            // This item# is the signal to exit the game.
  133.         ExitToShell();                        // Quit to Finder.
  134.     HideCursor();        // IMPORTANT NOTE: If you want the cursor to show when YellowAlert…
  135. }                        // …is finished, the program's next call should be ShowCursor().
  136.  
  137. //--------------------------------------------------------------  RedAlertString
  138.  
  139. // Generic error function.  This is called when there is no hope of recovering from the error.
  140. // A simple alert is brought up and the text passed in the StringPtr of some text imbedded…
  141. // …in the program is displayed.  When the user clicks the Okay button, we quit to the Finder.
  142.  
  143. void RedAlertString (StringPtr theString)
  144. {
  145.     short        doWhat;
  146.     
  147.     ParamText(theString, "\p", "\p", "\p");    // Replace ^0 in alert with error mssg.
  148. //    CenterAlert(rRedAlertID);                // Not needed with System 7.x.
  149.     InitCursor();                            // Show arrow cursor.
  150.     doWhat = Alert(rRedAlertID, 0L);        // Bring up alert.
  151.     ExitToShell();                            // Quit to Finder.
  152. }
  153.  
  154. //--------------------------------------------------------------  YellowAlertString
  155.  
  156. // This error function is called when the player has an option between quitting or continuing.
  157. // A simple alert is brought up and the text passed in the StringPtr of some text imbedded…
  158. // …in the program is displayed. When the user clicks the Finder button, we quit…
  159. // …to the Finder, when the user clicks the Play button, we continue to play.
  160.  
  161. void YellowAlertString (StringPtr theString)
  162. {
  163.     short        doWhat;
  164.     
  165.     ParamText(theString, "\p", "\p", "\p");    // Replace ^0 in alert with error mssg.
  166. //    CenterAlert(rRedAlertID);                // Not needed with System 7.x.
  167.     ShowCursor();                            // Make sure the cursor is showing.
  168.     doWhat = Alert(rYellowAlertID, 0L);        // Bring up alert, find out which item was chosen.
  169.     if (doWhat == kYellowExitItem)            // This item# is the signal to exit the game.
  170.         ExitToShell();                        // Quit to Finder.
  171.     HideCursor();        // IMPORTANT NOTE: If you want the cursor to show when YellowAlertString…
  172. }                        // …is finished, the program's next call should be ShowCursor().
  173.  
  174. //--------------------------------------------------------------  MacHasColorQD  
  175.  
  176. // Simple function that returns TRUE if we're running on a Mac that…
  177. // is running Color Quickdraw.
  178.  
  179. Boolean MacHasColorQD (void)
  180. {
  181.     SysEnvRec        thisWorld;
  182.     
  183.     SysEnvirons(2, &thisWorld);        // Call SysEnvirons() version 2.
  184.     return (thisWorld.hasColorQD);    // Return whether it has Color QuickDraw.
  185. }
  186.  
  187. //--------------------------------------------------------------  MacHasSystem7  
  188.  
  189. // Returns TRUE if the Mac we're running on has System 7.0.0 or more recent.
  190. // System 7 introduced…man, there are whole books on the subject.
  191. // If your game is running routines that weren't introduced until System 7.x, call this first.
  192. // You might also want to demand System 7 to save yourself hassles with compatability testing,…
  193. // …but many older Macs were never upgraded to System 7 because System 7 is a memory hog,…
  194. // …and many of those older Macs are in the kid's playroom now. I know many programmers feel…
  195. // …old Macs and System 6.x are ready for the dumpster and the cutting edge is where it's at,…
  196. // …but to paraphrase what Chinese mothers tell their kids when they won't eat their fried rice,…
  197. // …there are children in Appalachia who would loooove to have your Mac II with System 6.0.8.
  198.  
  199. Boolean MacHasSystem7 (void)
  200. {
  201.     SysEnvRec        thisWorld;
  202.     Boolean            haveIt;
  203.     
  204.     SysEnvirons(2, &thisWorld);        // Call the old SysEnvirons() function.
  205.     if (thisWorld.systemVersion >= 0x0700)
  206.         haveIt = TRUE;                // Check the System version for 6.0.2…
  207.     else                            // or more recent
  208.         haveIt = FALSE;
  209.     return (haveIt);
  210. }
  211.  
  212. //--------------------------------------------------------------  MacHasSystem605  
  213.  
  214. // Returns TRUE if the Mac we're running on has System 6.0.5 or more recent.
  215. // System 6.0.5 introduced the ability to switch color depths.
  216.  
  217. Boolean MacHasSystem605 (void)
  218. {
  219.     SysEnvRec        thisWorld;
  220.     Boolean            haveIt;
  221.     
  222.     SysEnvirons(2, &thisWorld);        // Call the old SysEnvirons() function.
  223.     if (thisWorld.systemVersion >= 0x0605)
  224.         haveIt = TRUE;                // Check the System version for 6.0.5…
  225.     else                            // or more recent
  226.         haveIt = FALSE;
  227.     return (haveIt);
  228. }
  229.  
  230. //--------------------------------------------------------------  MacHasSystem602  
  231.  
  232. // Returns TRUE if the Mac we're running on has System 6.0.2 or more recent.
  233. // System 6.0.2 introduced lots of good sound routines. This may be all you need.
  234.  
  235. Boolean MacHasSystem602 (void)
  236. {
  237.     SysEnvRec        thisWorld;
  238.     Boolean            haveIt;
  239.     
  240.     SysEnvirons(2, &thisWorld);        // Call the old SysEnvirons() function.
  241.     if (thisWorld.systemVersion >= 0x0602)
  242.         haveIt = TRUE;                // Check the System version for 6.0.2…
  243.     else                            // or more recent
  244.         haveIt = FALSE;
  245.     return (haveIt);
  246. }
  247.  
  248. //--------------------------------------------------------------  MacIsAtLeastMacII  
  249.  
  250. // Returns TRUE if the Mac we're running is a Mac II or more recent.
  251. // You may need more (or less), in which case this routine is easy to modify.
  252.  
  253. Boolean MacIsAtLeastMacII (void)
  254. {
  255.     SysEnvRec        thisWorld;
  256.     Boolean            haveIt;
  257.     
  258.     SysEnvirons(2, &thisWorld);        // Call the old SysEnvirons() function.
  259.     if (thisWorld.machineType >= envMacII)
  260.         haveIt = TRUE;                // Check the System version for 6.0.2…
  261.     else                            // or more recent
  262.         haveIt = FALSE;
  263.     return (haveIt);
  264. }
  265.  
  266. //--------------------------------------------------------------  FindOurDevice
  267.  
  268. // Get a handle to the MainDevice (monitor with the Menubar).
  269.  
  270. void FindOurDevice (void)
  271. {
  272.     gThisGDevice = GetMainDevice();
  273.     if (gThisGDevice == 0L)                        // If a nil handle is returned...
  274.         RedAlertString("\pCouldn't Find Our Device");    // call our universal error alert.
  275. }
  276.  
  277. //--------------------------------------------------------------  WhatsOurDepth  
  278.  
  279. // Function returns the current bit depth.  For example, 4 = 16 colors, 8 = 256…
  280. // colors.This function assumes System 6.0.5 or more recent and Color Quickdraw capable,…
  281. // …so you should haved checked before attempting this call (see routines above).
  282.  
  283. short WhatsOurDepth (void)
  284. {
  285.     short            thisDepth;
  286.     char            wasState;
  287.     
  288.     gThisGDevice = GetMainDevice();
  289.     if (gThisGDevice != 0L)                            // Make sure we have device handle.
  290.     {
  291.         wasState = HGetState((Handle)gThisGDevice);    // Remember the handle's state.
  292.         HLock((Handle)gThisGDevice);                    // Lock the device handle down.
  293.                                                     // Get it's depth (pixelSize).
  294.         thisDepth = (**(**gThisGDevice).gdPMap).pixelSize;
  295.         HSetState((Handle)gThisGDevice, wasState);    // Restore handle's state.
  296.     }
  297.     else
  298.         RedAlertString("\pUnknown Error.");                // Post generic error message.
  299.     
  300.     return (thisDepth);                                // Return screen depth.
  301. }
  302.  
  303. //--------------------------------------------------------------  CheckMacEnviro
  304.  
  305. // This is the "wrapper" function that calls the above functions.
  306. // After calling ToolBoxInit(), the program will call this function to see…
  307. // …if the current Mac we're running on is capable of running this program.
  308.  
  309. void CheckMacEnviro (void)
  310. {
  311.     if (!MacHasColorQD())            // If the game is in color, it needs Color QuickDraw.
  312.         RedAlert(kErrNoColor);
  313.     
  314.     if (!MacHasSystem7())            // Actually, System 6.0.5 would run this demo. If an…
  315.         RedAlert(kErrOldSystem);    // …earlier system will do for you, change this.
  316.                                     
  317.     if (!MacIsAtLeastMacII())        // Your game may need more or less hardware.
  318.         RedAlert(kErrOldMacintosh);    // Change this to suit your needs.
  319. }
  320.  
  321.  
  322. //--------------------------------------------------------------  InitToolbox
  323.  
  324. //                     A generic first function
  325. //
  326. //        The calls herein MUST be called before you do anything else.
  327. //        Otherwise, you'll get all sorts of System Errors.
  328.  
  329. void InitToolbox (void)
  330. {
  331.     InitGraf(&qd.thePort);        // Initialize QuickDraw variables for our program.
  332.     InitFonts();                // Initialize fonts.
  333.     FlushEvents(everyEvent, 0);    // Clear event queue of any pending events.
  334.     InitWindows();                // Initialize the Window Manager.
  335.     InitMenus();                // Ditto for the Menu Manager.
  336.     TEInit();                    // blah, blah Text Edit.
  337.     InitDialogs(0L);            // blah, blah Dialog Manager.
  338.     InitCursor();                // Set the cursor to the arrow cursor and init.
  339.     
  340.     MaxApplZone();                // Grab application memory.
  341.     
  342.     MoreMasters();                // Allocate a block of master pointers.
  343.     MoreMasters();                // And allocate more.
  344.     MoreMasters();                // And more.
  345.     MoreMasters();                // Hey, lets do it again too.
  346.     
  347.     GetDateTime((unsigned long *)&qd.randSeed);        // Randomize random seed.
  348. }
  349.  
  350. //------------------------------------------------------------------------------------------\\
  351. //                                    End MGWUtilities1.c                                        \\
  352. //------------------------------------------------------------------------------------------\\